package com.uraurora.crescent.function.functools.memoizer;

import com.google.common.cache.Cache;
import com.uraurora.crescent.function.Func2;
import com.uraurora.crescent.tuple.ITuple;
import com.uraurora.crescent.tuple.Tuple;

import java.util.concurrent.*;

public class MemoizerFunc2<T1, T2, RT> extends AbstractMemoizer<RT> implements Func2<T1, T2, RT> {

    private final Func2<? super T1, ? super T2, ? extends RT> computable;

    public MemoizerFunc2(Func2<? super T1, ? super T2, ? extends RT> computable, Cache<ITuple, Future<RT>> cache) {
        super(cache);
        this.computable = computable;
    }

    public MemoizerFunc2(Func2<? super T1, ? super T2, ? extends RT> computable){
        super();
        this.computable = computable;
    }

    @Override
    public RT on(T1 t1, T2 t2) {
        return compute(Tuple.of(t1, t2), ()->computable.on(t1, t2));
    }

    @Override
    @Deprecated
    public MemoizerFunc2<T1, T2, RT> memoize(){
        throw new UnsupportedOperationException();
    }

    @Override
    @Deprecated
    public MemoizerFunc2<T1, T2, RT> memoize(Cache<ITuple, Future<RT>> cache){
        throw new UnsupportedOperationException();
    }
}
